<?php
/*+---------------------------------------------------+
  |       内容管理                                    |
  +---------------------------------------------------+*/
class WebsiteContentManageAction extends CommonAction{
    /*+---------------------------------------------+
      |     首页                                    |
      +---------------------------------------------+*/
    public function index(){
           $this->assign("columnName",$this->getColumnSelect());
           if($_POST['show']){
               //根据栏目类型跳转栏目主页面
               if($_POST['column_id']){
                      $column = $this->getColumn($_POST['column_id']);
                      $this->redirect("WebsiteContentManage/{$column['column_type']}",array("id"=>$column['id']));
               }
           }
           $this->display();
    }

    //获取栏目下拉框
    public function getColumnSelect(){
           //切换数据库
           $this->changeDB();
           $col    = D("website_column");
           $column = $col ->where("column_tablename != '' and status = '1'") ->select();
           return $column;
    }

    //获取站点栏目信息
    public function getColumn($operate){
           $this->changeDB();
           $col    = D("website_column");
           $column = $col ->where("id = {$operate}")->find();
           return $column;
    }

    //页面 =》关于我们主页(根据post的键名add or edit确定是添加还是修改)
    public function aboutUs(){
	       if($_POST['save']){
		           //切换数据库
                   $this->saveArticle($_POST);		   
		   }
		   
	       $this->assign("columnName",$this->getColumnSelect());
		   $this->assign("articleInfo",$this->findArticle($_GET['id']));
           $this->assign("column_id",$_GET['id']);
           $this->display();
    }

    //保存文章
    public function saveArticle($_POST){
           $_POST['article_describe'] = str_replace('\"','"',$_POST['article_describe']);
           $_POST['article_content']  = str_replace('\"','"',$_POST['article_content']);
		   $this->changeDB();
		   $article = D("website_article");
		   $action='add';
		   if($_POST['id']){
		   		$action='update';
				 $save = $article ->save($_POST);
		   }else{
				 $save = $article ->add($_POST);
		   }
		   if($save){
		   	     //---操作统计--日志记录-------------------------------
	                 updateStaticAndLog($_POST['column_id'],$action,intval($_POST['id']),'');
	           	 //----------------------------------------------------
				 $this->success("保存成功！");
		   }else{
				 $this->error("保存失败！");
		   }
    }
 
    //搜索文章
	public function findArticle($cid){
	       //切换数据库
		   $this->changeDB();
		   $article = D("website_article");
		   $art     = $article ->where("column_id = {$cid}")->find();
		   return $art;
	}
    //页面=》新闻中心首页
    public function newsCenter(){
           $this->assign("columnName",$this->getColumnSelect());
           //显示该栏目下的新闻
           $this->changeDB();
           $news       = D("website_news");
           if($_GET['search']){
                   $where = "column_id = {$_GET['column_id']} and status='1' and news_name like '%{$_GET['keyWord']}%'";
                   $_GET['id'] = $_GET['column_id'];
           }else{
                   $where = "column_id = {$_GET['id']} and status='1'";
           }
           $limit      = $this -> pageOperate("website_news",20,$where);
           $newsColumn = $news -> where($where) ->limit("{$limit['firstRow']},{$limit['listRows']}")->order("id desc") -> select();
           $this->assign("newsColumn",$newsColumn);
           $this->assign("column_id",$_GET['id']);
           $this->display();
    }
    //页面 =》添加新闻
    public function addNewsCenter(){
           if($_POST['save']){
                    $this->addNews("addNewsCenter");
           }
           $this->assign("columnName",$this->getColumnSelect());
           if($_GET['column_id']){
               $this->assign("column_id",$_GET['column_id']);
           }
           if($_POST['column_id']){
               $this->assign("column_id",$_POST['column_id']);
           }
           $this->display();
    }

    //添加新闻
    public function addNews($app){
           $_POST['news_describe'] = str_replace('\"','"',$_POST['news_describe']);
           $_POST['news_content']  = str_replace('\"','"',$_POST['news_content']);
           //echo "<pre>";print_r($_POST);exit;
           //切换数据库
           $this ->changeDB();
           $news = D("website_news");
           $add  = $news ->add($_POST);
           if($add){
           		 //---操作统计--日志记录-------------------------------
           		 updateStaticAndLog(intval($_POST['column_id']),'add',$add,'');
           	     //------------------------------------
                 $this->assign("jumpUrl",U("WebsiteContentManage/{$app}/column_id/{$_POST['column_id']}"));
                 $this->success("添加成功！");
           }else{
                 $this->assign("jumpUrl",U("WebsiteContentManage/{$app}/column_id/{$_POST['column_id']}"));
                 $this->error("添加失败！");
           }
    }
    //页面 =》编辑新闻
    public function manageNews(){
           //echo "<pre>";print_r($_POST);exit;
           if($_POST['save']){
               $this->saveNews($_POST,"newsCenter");
           }
           $this->assign("columnName",$this->getColumnSelect());
           if($_GET['column_id']){
               $this->assign("column_id",$_GET['column_id']);
           }
           if($_POST['column_id']){
               $this->assign("column_id",$_POST['column_id']);
           }
           if($_POST['edit']){
               if(count($_POST['id'])>0){
                        $in    = $this ->arrayToString($_POST['id']);
                        $where = "id in ({$in}) and status='1'";
               }else{
                        $this->ifChangeCheckbox($_POST['column_id']);
               }
           }
           
           if($_GET['edit']){
               $where = "id = {$_GET['edit']} and status='1'";
           }

           if($_POST['del']){
                 if(count($_POST['id'])>0){
                     $in    = $this ->arrayToString($_POST['id']);
                     $where = "id in ({$in})";
                     $this->delContent($where,$_POST['column_id']);
                 }else{
                     $this->ifChangeCheckbox($_POST['column_id']);
                 }
           }

           if($_GET['del']){
                 $where = "id = {$_GET['del']}";
                 $this->delContent($where,$_GET['column_id']);
           }
           $this       ->changeDB();
           $news       = D("website_news"); 
           $newsResult = $news ->where($where)->order("id desc")->select();
           $this       ->assign("newsResult",$newsResult);
           $this->display();
    }

    //是否勾选要管理的项
    public function ifChangeCheckbox($column_id){
               $column = $this->getColumn($column_id);
               $this->assign("jumpUrl",U("WebsiteContentManage/{$column['column_type']}/id/{$column_id}"));
               $this->error("请勾选要编辑的内容项！");
    }
    //保存=》新闻

    public function saveNews($newsInfo,$app){
           $this ->changeDB();
           $newNewsInfo = $this->arraySwitch($newsInfo);
           $news = D("website_news");
           foreach($newNewsInfo as $key => $val){
                  $val['news_describe'] = str_replace('\"','"',$val['news_describe']);
                  $val['news_content']  = str_replace('\"','"',$val['news_content']);
                  $save +=  $news -> save($val);
                  //---操作统计--日志记录-------------------------------
                  updateStaticAndLog(intval($_POST['column_id']),'update',$val['id'],'');
           	      //----------------------------------------------------
           }
           //exit;
           if($save){
                  $this ->assign("jumpUrl",U("WebsiteContentManage/{$app}/id/{$newsInfo['column_id']}"));
                  $this ->success("成功的修改了 {$save} 个新闻！");
           }else{
                  $this ->error("修改新闻失败！");
           }
           
    }
    //页面=》产品中心首页
    public function productArrondi(){
           $this->changeDB();
           $news       = D("website_product");
           if($_GET['search']){
                   $where = "column_id = {$_GET['column_id']} and status='1' and product_name like '%{$_GET['keyWord']}%'";
                   $_GET['id'] = $_GET['column_id'];
           }else{
                   $where = "column_id = {$_GET['id']} and status='1'";
           }
           $limit      = $this -> pageOperate("website_product",20,$where);
           $productColumn = $news -> where($where)-> limit("{$limit['firstRow']},{$limit['listRows']}")->order("id desc") -> select();
           $this->assign("productColumn",$productColumn);
	       $this->assign("columnName",$this->getColumnSelect());
           $this->assign("column_id",$_GET['id']);
           $this->display();
    }
	
    //页面 =》添加产品
    public function addProductArrondi(){
           if($_POST['save']){
                    foreach($_POST['colors'] as $key => $val){
                           $color .= $val."|";
                    }       
                    $_POST['product_color']    = rtrim($color,"|");
                    $_POST['product_describe'] = str_replace('\"','"',$_POST['product_describe']);
                    //echo "<pre>";print_r($_POST);exit;
                    //切换数据库
                    $this ->changeDB();
                    $product = D("website_product");
                    $add  = $product ->add($_POST);
                    if($add){
                    	   //---操作统计--日志记录-------------------------------
		                      updateStaticAndLog($_POST['column_id'],'add',$add);
		           	       //----------------------------------------------------
                           $this->assign("jumpUrl",U("WebsiteContentManage/addProductArrondi/column_id/{$_POST['column_id']}"));
                           $this->success("添加成功！");
                    }else{
                           $this->assign("jumpUrl",U("WebsiteContentManage/addProductArrondi/column_id/{$_POST['column_id']}"));
                           $this->error("添加失败！");
                    }
           }
           $this->assign("columnName",$this->getColumnSelect());
           if($_GET['column_id']){
               $this->assign("column_id",$_GET['column_id']);
           }
           if($_POST['column_id']){
               $this->assign("column_id",$_POST['column_id']);
           }
           $this->display();
    }

    //管理产品
    public function saveProductArrondi($newsInfo,$app){
           $this ->changeDB();
           $newNewsInfo = $this->arraySwitch($newsInfo);
           $news = D("website_product");
           foreach($newNewsInfo as $key => $val){
                  $val['product_describe'] = str_replace('\"','"',$val['product_describe']);
                  $save += $news -> save($val);
                  //---操作统计--日志记录-------------------------------
		          		updateStaticAndLog($_POST['column_id'],'update',$val['id']);
		          //----------------------------------------------------
           }
           //exit;
           if($save){
                  $this ->assign("jumpUrl",U("WebsiteContentManage/{$app}/id/{$newsInfo['column_id']}"));
                  $this ->success("成功的修改了 {$save} 个产品信息！");
           }else{
                  $this ->error("修改产品信息失败！");
           }
           
    }

    //页面 =》管理产品
    public function manageProductArrondi(){
           if($_POST['save']){
               $postArray = $_POST;
               foreach($postArray['colors'] as $key => $val){
                   foreach($val as $k => $v){
                       if($v != ""){   //过滤掉没选择颜色的值
                           $str .= $v."|";
                       }else{
                           $str .= $v;
                       }
                   }
                   $arr[$key] = rtrim($str,"|");
                   $str = "";
               }
               $_POST = "";
               foreach($postArray as $key => $val){
                    if($key != "colors"){
                          $_POST[$key] = $val;
                    }
               }
               foreach($arr as $key => $val){
                    $colorsArray[] = $val; 
               }
               $_POST['product_color'] = $colorsArray;
               //echo "<pre>";print_r($_POST);print_r($arr);exit;
               $this->saveProductArrondi($_POST,"productArrondi");
           }
           $this->assign("columnName",$this->getColumnSelect());
           if($_GET['column_id']){
               $this->assign("column_id",$_GET['column_id']);
           }
           if($_POST['column_id']){
               $this->assign("column_id",$_POST['column_id']);
           }
           if($_POST['edit']){
               if(count($_POST['id'])>0){
                        $in    = $this ->arrayToString($_POST['id']);
                        $where = "id in ({$in}) and status='1'";
               }else{
                        $this->ifChangeCheckbox($_POST['column_id']);
               }
           }
           
           if($_GET['edit']){
               $where = "id = {$_GET['edit']} and status='1'";
           }

           if($_POST['del']){
                 if(count($_POST['id'])>0){
                     $in    = $this ->arrayToString($_POST['id']);
                     $where = "id in ({$in})";
                     $this->delContent($where,$_POST['column_id']);
                 }else{
                     $this->ifChangeCheckbox($_POST['column_id']);
                 }
           }

           if($_GET['del']){
                 $where = "id = {$_GET['del']}";
                 $this->delContent($where,$_GET['column_id']);
           }
           $this             ->changeDB();
           $product          = D("website_product"); 
           $Result           = $product ->where($where)->order("id desc")->select();
           $productResult    = $this->colorNewArray($Result);
           $this             ->assign("productResult",$productResult);
           $this             ->assign("num",count($Result));
           $this->display();
    }

    private function colorNewArray($array){
            foreach($array as $key => $val){
                    foreach($val as $k => $v){
                            if($k == "product_color"){
                                  $array[$key][$k] = explode("|",$v);
                            }
                    }
                    
                    //echo "<pre>";print_r($array);exit;
            }      
            return $array; 
    }
    //页面=》产品中心首页
    public function shopAddress(){
           $this->changeDB();
           $news           = D("website_address");
           if($_GET['search']){
                   $where = "column_id = {$_GET['column_id']} and status='1' and shop_name like '%{$_GET['keyWord']}%'";
                   $_GET['id'] = $_GET['column_id'];
           }else{
                   $where = "column_id = {$_GET['id']} and status='1'";
           }
           $limit      = $this -> pageOperate("website_address",20,$where);
           $addresstColumn = $news -> where($where) ->limit("{$limit['firstRow']},{$limit['listRows']}")->order("id desc") -> select();
           $this->assign("addresstColumn",$addresstColumn);
	       $this->assign("columnName",$this->getColumnSelect());
           $this->assign("column_id",$_GET['id']);
           $this->display();
    }
	
    //页面 =》添加商店地址
    public function addShopAddress(){
           if($_POST['save']){
                    $_POST['shop_describe'] = str_replace('\"','"',$_POST['shop_describe']);
                    $_POST['shop_content']  = str_replace('\"','"',$_POST['shop_content']);
                    //echo "<pre>";print_r($_POST);exit;
                    //切换数据库
                    $this ->changeDB();
                    $news = D("website_address");
                    $add  = $news ->add($_POST);
                    if($add){
                    	   //---操作统计--日志记录-------------------------------
				          		updateStaticAndLog($_POST['column_id'],'add',$add);
				           //----------------------------------------------------
                           $this->assign("jumpUrl",U("WebsiteContentManage/addShopAddress/column_id/{$_POST['column_id']}"));
                           $this->success("添加成功！");
                    }else{
                           $this->assign("jumpUrl",U("WebsiteContentManage/addShopAddress/column_id/{$_POST['column_id']}"));
                           $this->error("添加失败！");
                    }
           }
           $this->assign("columnName",$this->getColumnSelect());
           if($_GET['column_id']){
               $this->assign("column_id",$_GET['column_id']);
           }
           if($_POST['column_id']){
               $this->assign("column_id",$_POST['column_id']);
           }
           $this->display();
    }

    //管理产品
    public function saveShopAddress($newsInfo,$app){
           $this ->changeDB();
           $newNewsInfo = $this->arraySwitch($newsInfo);
           $news = D("website_address");
           foreach($newNewsInfo as $key => $val){
                  $val['shop_describe'] = str_replace('\"','"',$val['shop_describe']);
                  $val['shop_content']  = str_replace('\"','"',$val['shop_content']);
                  $save += $news -> save($val);
                  //---操作统计--日志记录-------------------------------
				       updateStaticAndLog($_POST['column_id'],'update',$aval['id']);
				  //----------------------------------------------------
           }
           //exit;
           if($save){
                  $this ->assign("jumpUrl",U("WebsiteContentManage/{$app}/id/{$newsInfo['column_id']}"));
                  $this ->success("成功的修改了 {$save} 个商店地址！");
           }else{
                  $this ->error("修改商店地址失败！");
           }
           
    }

    //页面 =》编辑商店地址
    public function manageShopAddress(){
           if($_POST['save']){
               $this->saveShopAddress($_POST,"shopAddress");
           }
           $this->assign("columnName",$this->getColumnSelect());
           if($_GET['column_id']){
               $this->assign("column_id",$_GET['column_id']);
           }
           if($_POST['column_id']){
               $this->assign("column_id",$_POST['column_id']);
           }
           if($_POST['edit']){
               if(count($_POST['id'])>0){
                        $in    = $this ->arrayToString($_POST['id']);
                        $where = "id in ({$in}) and status='1'";
               }else{
                        $this->ifChangeCheckbox($_POST['column_id']);
               }
           }
           
           if($_GET['edit']){
               $where = "id = {$_GET['edit']} and status='1'";
           }

           if($_POST['del']){
                 if(count($_POST['id'])>0){
                     $in    = $this ->arrayToString($_POST['id']);
                     $where = "id in ({$in})";
                     $this->delContent($where,$_POST['column_id']);
                 }else{
                     $this->ifChangeCheckbox($_POST['column_id']);
                 }
           }

           if($_GET['del']){
                 $where = "id = {$_GET['del']}";
                 $this->delContent($where,$_GET['column_id']);
           }
           $this          ->changeDB();
           $news          = D("website_address"); 
           $addressResult = $news ->where($where)->order("id desc")->select();
           $this          ->assign("addressResult",$addressResult);
           $this          ->display();
    }
	
    //页面 =》招商合作首页
    public function canvassBusinessOrders(){
           if($_POST['save']){
		           //切换数据库
                   $this->saveArticle($_POST);		   
		   }
           $this->assign("columnName",$this->getColumnSelect());
           $this->assign("articleInfo",$this->findArticle($_GET['id']));
           $this->assign("column_id",$_GET['id']);
           $this->display();
    }

    //页面 =》服务中心首页
    public function serviceCentre(){
           if($_POST['save']){
		           //切换数据库
                   $this->saveArticle($_POST);		   
		   }
           $this->assign("columnName",$this->getColumnSelect());
           $this->assign("articleInfo",$this->findArticle($_GET['id']));
           $this->assign("column_id",$_GET['id']);
           $this->display();
    }
	
	//页面 =》人才招聘首页
    public function personnelRecruitment(){
           $this->assign("columnName",$this->getColumnSelect());
           //显示该栏目下的新闻
           $this->changeDB();
           $news       = D("website_news");
           if($_GET['search']){
                   $where = "column_id = {$_GET['column_id']} and status='1' and news_name like '%{$_GET['keyWord']}%'";
                   $_GET['id'] = $_GET['column_id'];
           }else{
                   $where = "column_id = {$_GET['id']} and status='1'";
           }
           $limit      = $this -> pageOperate("website_news",20,$where);
           $newsColumn = $news -> where($where) ->limit("{$limit['firstRow']},{$limit['listRows']}")->order("id desc") -> select();
           $this->assign("newsColumn",$newsColumn);
           $this->assign("column_id",$_GET['id']);
           $this->display();
    }
	
    //页面 =》添加人才招聘
    public function addPersonnelRecruitment(){
	       if($_POST['save']){
                    $this->addNews("addPersonnelRecruitment");
           }
           $this->assign("columnName",$this->getColumnSelect());
           if($_GET['column_id']){
               $this->assign("column_id",$_GET['column_id']);
           }
           if($_POST['column_id']){
               $this->assign("column_id",$_POST['column_id']);
           }
           $this->display();
    
    }

    //页面 =》编辑人才招聘
    public function managePersonnelRecruitment(){
           //echo "<pre>";print_r($_POST);exit;
           if($_POST['save']){
               $this->saveNews($_POST,"personnelRecruitment");
           }
           $this->assign("columnName",$this->getColumnSelect());
           if($_GET['column_id']){
               $this->assign("column_id",$_GET['column_id']);
           }
           if($_POST['column_id']){
               $this->assign("column_id",$_POST['column_id']);
           }
           if($_POST['edit']){
               if(count($_POST['id'])>0){
                        $in    = $this ->arrayToString($_POST['id']);
                        $where = "id in ({$in}) and status='1'";
               }else{
                        $this->ifChangeCheckbox($_POST['column_id']);
               }
           }
           
           if($_GET['edit']){
               $where = "id = {$_GET['edit']} and status='1'";
           }

           if($_POST['del']){
                 if(count($_POST['id'])>0){
                     $in    = $this ->arrayToString($_POST['id']);
                     $where = "id in ({$in})";
                     $this->delContent($where,$_POST['column_id']);
                 }else{
                     $this->ifChangeCheckbox($_POST['column_id']);
                 }
           }

           if($_GET['del']){
                 $where = "id = {$_GET['del']}";
                 $this->delContent($where,$_GET['column_id']);
           }
           $this       ->changeDB();
           $news       = D("website_news"); 
           $newsResult = $news ->where($where)->order("id desc")->select();
           $this       ->assign("newsResult",$newsResult);
           $this->display();
    }
    
    //页面 =》联系我们首页
    public function connectUs(){
           if($_POST['save']){
		           //切换数据库
                   $this->saveArticle($_POST);		   
		   }
           $this->assign("columnName",$this->getColumnSelect());
           $this->assign("articleInfo",$this->findArticle($_GET['id']));
           $this->assign("column_id",$_GET['id']);
           $this->display();
    }

    //删除 =》操作
    public function delContent($where,$column_id){
           
           $columnInfo = $this->getColumn($column_id);
           $this       -> changeDB();
           $table      = D("{$columnInfo['column_tablename']}");
           
           $del        = $table ->where($where)->delete();
           if($del){
           	         //---操作统计--日志记录-------------------------------
	                  updateStaticAndLog($column_id,'delete',0,$where);
	           	     //----------------------------------------------------
                    $this ->assign('jumpUrl',U("WebsiteContentManage/".$columnInfo['column_type']."/id/".$column_id));
                    $this ->success("成功的删除了 {$del} 个信息！");
           }else{
                    $this ->assign('jumpUrl',U("WebsiteContentManage/".$columnInfo['column_type']."/id/".$column_id));
                    $this ->error("删除失败！");
           }

    }
    
}
?>
